﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebShop.Models;
using WebShop.Classes;
using WebShop.DataService;
using WebShop.Classes.DataSource;
using System.Web.Routing;
using WebShop.Classes.Messaging;

namespace WebShop.Controllers
{
    public class CartController : BaseController
    {

        /// <summary>
        /// Convert from full product to View
        /// </summary>
        /// <param name="item">Full product class</param>
        /// <returns>View product class</returns>
        private ProductView FullProductToView(ProductFull item)
        {
            return new ProductView()
            {
                Id          = item.Id,
                CategoryId  = item.CategoryId,
                Price       = item.Price,
                Photo       = item.Photo,
                Name        = item.Name,
                Count       = item.Count
            };
        }

        public ActionResult Index()
        {
            CartModel model = new CartModel();
            model.Title = Translater.Translate("title_cart");

            return View(model);
        }

        [Authorize]
        public ActionResult History()
        {
            IDataSource dataSource = DataSourceFactory.Contruct();
            HistoryModel model = new HistoryModel();
            model.Title = Translater.Translate("title_history");
            model.Orders = dataSource.GetOrders();
            return View("History", model);
        }

        [AcceptVerbs(HttpVerbs.Get)]
        [Authorize]
        public ActionResult HistoryItem(long id)
        {
            IDataSource dataSource = DataSourceFactory.Contruct();
            HistoryItemModel model = new HistoryItemModel();
            model.Order = dataSource.GetOrder(id);
            model.Title = Translater.Translate("title_history_item");
            if (model.Order == null)
            {
                return Error404();
            }
            return View("HistoryItem", model);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        [Authorize]
        public ActionResult Accept()
        {
            IDataSource dataSource = DataSourceFactory.Contruct();

            //if we have items
            if (ConfigStorage.Order.Composition.Count != 0)
            {
                ConfigStorage.Order.PlaceId = Int32.Parse(Request.Form["place"]);
                OrderFull order = dataSource.SetOrder(ConfigStorage.Order);
                if (order != null)
                {
                    ConfigStorage.ClearOrder();
                    return RedirectToAction("historyitem", "cart", new RouteValueDictionary { { "id", order.Id } });
                }
                ConfigStorage.Order.PlaceId = 0;
            }

            return RedirectToAction("Index", "Cart");
        }

        /// <summary>
        /// Add product to cart
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Add(long id, int count = 1)
        {
            IDataSource dataSource = DataSourceFactory.Contruct();
            ProductFull product = dataSource.GetProduct(id);
            if (product != null) {
             //   bool hasItem = false;

                for (int i = 0; i < ConfigStorage.Order.Composition.Count; i++)
                {
                    if (ConfigStorage.Order.Composition[i].Product.Id == id)
                    {
                        ConfigStorage.Order.Composition[i].Count += count;
                        return RedirectToAction("Index", "Cart");
                    }
                }

                ConfigStorage.Order.Composition.Add(new OrderPosition()
                {
                            Product = FullProductToView(product),
                            Count = count,
                            Cost = product.Price
                });

                ConfigStorage.Messages.Add(new Message()
                {
                    Text = String.Format(Translater.Translate("item_{0}_x_{1}_add_success"), product.Name, count),
                    Type = MessageType.Info
                });
            }
            return RedirectToAction("Index", "Cart");
        }

        /// <summary>
        /// Add product to cart
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Drop(long id, int count = 1)
        {
            IDataSource dataSource = DataSourceFactory.Contruct();

            for (int i = 0; i < ConfigStorage.Order.Composition.Count; i++)
            {
                if (ConfigStorage.Order.Composition[i].Product.Id == id)
                {
                    ConfigStorage.Order.Composition[i].Count -= count;
                    if (ConfigStorage.Order.Composition[i].Count <= 0)
                    {
                        ConfigStorage.Messages.Add(new Message()
                        {
                            Text = String.Format(Translater.Translate("item_{0}_x_{1}_drop_success"), ConfigStorage.Order.Composition[i].Product.Name, count),
                            Type = MessageType.Info
                        });
                        ConfigStorage.Order.Composition.RemoveAt(i);
                    }

                    return RedirectToAction("Index", "Cart");
                }
            }

            return RedirectToAction("Index", "Cart");
        }
        
    }
}
